#include "ht.h"

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main(int argc, char **argv) {
  if (argc != 3) {
    cerr << "Usage " << argv[0]
	 << " index_file image_file" << endl;
    return 0;
  }

  fstream fi_in;
  fi_in.open(argv[1],ios::in); // modo lectura
  if (!fi_in) {
    cerr << "Error opening " << argv[1] << endl;
    return 0;
  }

  hash_table H(hash_division,20,1.0);

  const int SIZE=512;
  char f_image[SIZE], description[SIZE];
  const char *pdescription;
  while (fi_in.getline(f_image,SIZE,' ') &&
	 fi_in.getline(description,SIZE)) {
    cout << "Loading " << f_image << endl;
    fstream fi_image(f_image,ios::in); // modo lectura
    if (!fi_image) {
      cerr << "Error al abrir fichero " << f_image << endl;
      exit(1);
    }
    ImagenColor * ima = new ImagenColor(fi_image);
    fi_image.close();
    H.insert(ima, description);
    delete ima;
  }
  fi_in.close();

  fstream fi_image(argv[2],ios::in);
  ImagenColor * ima = new ImagenColor(fi_image);
  fi_image.close();

  if (H.search(ima, pdescription))
    cout << pdescription << endl;
  else
    cout << "Imagen desconocida" << endl;
  delete ima;

  return 0;
}

